/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.server.domain;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.solarchallenge.shared.ApplicantStatus;
import com.google.solarchallenge.shared.dtos.ApplicantDto;
import com.google.solarchallenge.shared.exceptions.InvalidClientInputException;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


/**
 * DomainObject for Applicant.
 *
 * @author Arjun Satyapal
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION,
    detachable = "true")
public class ApplicantDao implements AbstractDao {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;
  @Persistent(dependent="true")
  private UsAddressDao usAddress;
  @Persistent
  private ApplicantStatus applicantStatus;

  // Reference to Parent Entity.
  @Persistent(mappedBy = "applicantDao")
  private UserAccountDao userAccount;

  private ApplicantDao() {
  }

  @Override
  public StringBuilder getStringBuilder() {
    return usAddress.getStringBuilder().append(applicantStatus.name());
  }

  @Override
  public String toString() {
    return getStringBuilder().toString();
  }

  @Override
  public String validate() {
    StringBuilder builder = new StringBuilder("");

    if (usAddress == null) {
      builder.append("usAddress is null.\n");
    }

    return builder.toString();
  }

  public String getEncodedKey() {
    return KeyFactory.keyToString(key);
  }

  public UserAccountDao getUserAccount() {
    return userAccount;
  }

  public UsAddressDao getUsAddress() {
    return usAddress;
  }

  public ApplicantStatus getApplicantStatus() {
    return applicantStatus;
  }

  public void setApplicantStatus(ApplicantStatus applicantStatus) {
    this.applicantStatus = applicantStatus;
  }

  public static ApplicantDao fromDto(ApplicantDto dto, boolean isCreate) {
    if (!dto.validate().isEmpty()) {
      throw new InvalidClientInputException(
          "Invalid ApplicantDao with Error : [" + dto.validate() + "].");
    }

    if (isCreate && dto.getKey() != null) {
      throw new InvalidClientInputException(
          "Invalid Key[" + dto.getKey() + "]. Only server is allowed to set "
              + "the id. Clients should not set it.");
    }

    if (dto.getApplicantStatus() != null) {
      throw new InvalidClientInputException(
          "Invalid applicantStatus[" + dto.getApplicantStatus()
          + "]. Only server is allowed to set the applicantStatus."
          + " Clients should not set it.");
    }

    ApplicantDao applicantDao = new ApplicantDao();
    applicantDao.usAddress = UsAddressDao
        .fromDto(dto.getUsAddressDto(), true /* isCreate */);

    if (!isCreate) {
      if (dto.getKey() == null) {
        throw new InvalidClientInputException("Id is not set for EditRequest.");
      }
      applicantDao.key = KeyFactory.stringToKey(dto.getKey());
    }

    return applicantDao;
  }

  public ApplicantDto toDto() {
    return new ApplicantDto.Builder().setKey(KeyFactory.keyToString(key))
        .setUsAddressDto(usAddress.toDto()).setApplicantStatus(applicantStatus)
        .build();
  }
}
